﻿#include "FITKRadiossDataResultRequestAdaptor.h"
#include "FITK_Kernel/FITKCore/FITKEnumTransformer.hpp"
#include "FITK_Interface/FITKInterfaceIO/FITKAbstractHDF5Writer.h"
#include "FITK_Interface/FITKInterfaceIO/FITKAbstractHDF5Reader.h"
#include "FITK_Interface/FITKInterfaceIO/FITKHDF5FileTool.h"
//RadiossData
#include "FITK_Component/FITKRadiossData/FITKRadiossResultRequest.h"

namespace Interface
{
    bool FITKRadiossDataResultRequestAdaptor::adaptR()
    {
        _resultRequest = dynamic_cast<Radioss::FITKRadiossResultRequest*>(_dataObj);
        if (_reader == nullptr || _resultRequest == nullptr || _h5Group == nullptr) return false;
        IO::FITKHDF5FileTool* h5tools = _reader->getHDF5FileTool();
        if (h5tools == nullptr) return false;

        double doubleValue = 0.0;
        std::string strValue;

        // 读取基本参数
        if (!h5tools->readGroupDouble(*_h5Group, "StartTime", &doubleValue)) return false;
        _resultRequest->setStartTime(doubleValue);

        if (!h5tools->readGroupDouble(*_h5Group, "Frequency", &doubleValue)) return false;
        _resultRequest->setFrequency(doubleValue);

        // 读取节点动画类型
        if (h5tools->readGroupStr(*_h5Group, "NodalAnimations", strValue))
        {
            QString nodalAnimsStr = QString::fromStdString(strValue);
            QStringList nodalAnimList = nodalAnimsStr.split(";", Qt::SkipEmptyParts);
            QList<Radioss::FITKRadiossResultRequest::NodalOutputAnimationsType> nodalAnims;

            Core::FITKEnumTransfer<Radioss::FITKRadiossResultRequest::NodalOutputAnimationsType> nodalTransfer;
            bool isValid = false;

            for (const QString& animStr : nodalAnimList)
            {
                Radioss::FITKRadiossResultRequest::NodalOutputAnimationsType animType =
                    nodalTransfer.fromString(animStr.trimmed(), isValid);
                if (isValid)
                {
                    nodalAnims.append(animType);
                }
            }
            _resultRequest->setNodalOutputAnim(nodalAnims);
        }

        // 读取单元动画类型
        if (h5tools->readGroupStr(*_h5Group, "ElementalAnimations", strValue))
        {
            QString elementalAnimsStr = QString::fromStdString(strValue);
            QStringList elementalAnimList = elementalAnimsStr.split(";", Qt::SkipEmptyParts);
            QList<Radioss::FITKRadiossResultRequest::ElementalOutputAnimationsType> elementalAnims;

            Core::FITKEnumTransfer<Radioss::FITKRadiossResultRequest::ElementalOutputAnimationsType> elementalTransfer;
            bool isValid = false;

            for (const QString& animStr : elementalAnimList)
            {
                Radioss::FITKRadiossResultRequest::ElementalOutputAnimationsType animType =
                    elementalTransfer.fromString(animStr.trimmed(), isValid);
                if (isValid)
                {
                    elementalAnims.append(animType);
                }
            }
            _resultRequest->setElementalOutputAnim(elementalAnims);
        }
        return true;
    }

    bool FITKRadiossDataResultRequestAdaptor::adaptW()
    {
        _resultRequest = dynamic_cast<Radioss::FITKRadiossResultRequest*>(_dataObj);
        if (_writer == nullptr || _resultRequest == nullptr || _h5Group == nullptr) return false;
        IO::FITKHDF5FileTool* h5tools = _writer->getHDF5FileTool();
        if (h5tools == nullptr) return false;
        //获取数据
       // 写入基本参数
        h5tools->writeGroupAttrDouble(*_h5Group, "StartTime", _resultRequest->getStartTime());
        h5tools->writeGroupAttrDouble(*_h5Group, "Frequency", _resultRequest->getFrequency());

        // 写入节点动画类型
        QList<Radioss::FITKRadiossResultRequest::NodalOutputAnimationsType> nodalAnims =
            _resultRequest->getNodalOutputAnim();
        QStringList nodalAnimStrs;

        Core::FITKEnumTransfer<Radioss::FITKRadiossResultRequest::NodalOutputAnimationsType> nodalTransfer;
        bool isValid = false;

        for (const Radioss::FITKRadiossResultRequest::NodalOutputAnimationsType& anim : nodalAnims)
        {
            QString animStr = nodalTransfer.toString(anim, isValid);
            if (isValid)
            {
                nodalAnimStrs.append(animStr);
            }
        }
        h5tools->writeGroupAttrStr(*_h5Group, "NodalAnimations", nodalAnimStrs.join(";").toStdString());

        // 写入单元动画类型
        QList<Radioss::FITKRadiossResultRequest::ElementalOutputAnimationsType> elementalAnims =
            _resultRequest->getElementalOutputAnim();
        QStringList elementalAnimStrs;

        Core::FITKEnumTransfer<Radioss::FITKRadiossResultRequest::ElementalOutputAnimationsType> elementalTransfer;
        isValid = false;

        for (const auto& anim : elementalAnims)
        {
            QString animStr = elementalTransfer.toString(anim, isValid);
            if (isValid)
            {
                elementalAnimStrs.append(animStr);
            }
        }
        h5tools->writeGroupAttrStr(*_h5Group, "ElementalAnimations", elementalAnimStrs.join(";").toStdString());
        return true;
    }
}
